<template>
  <a-directory-tree
    :tree-data="treeData"
    :selected-keys="defaultSelectedKeys"
    :default-expanded-keys="defaultExpandedKeys"
    @expand="onExpand"
    @select="onSelect"
  />
</template>
<script>
import { getTreeList } from '@/api/okms/department'
export default {
   // 声明当前子组件接收父组件传递的属性
  props: {
    parent_id: {
      type: String,
      default: '',
    },
  },
  data () {
    return {
      defaultSelectedKeys: ['0'],
      defaultExpandedKeys: ['0'],
      expandedKeys: [],
      treeData: [],
    }
  },
  mounted () {
    this.getTreeList({ FullName: this.parent_id })
  },
  methods: {
    onSelect (keys, info) {
      console.log('Trigger Select', keys, info)
     // console.log(info);
      // 触发父组件中 selectTree
      this.$emit('selectTree', keys[0], info.node.title, info.node.value)
    },
    unselect () {
      console.log('wo bei fq tiao yong ')
      this.defaultSelectedKeys = ['0']
    },
    onExpand () {
      // console.log("Trigger Expand");
    },
    getTreeList (params) {
      getTreeList(params).then((response) => {
        const treeData = this.fommat({
          arrayList: response.data.data.list,
          pidStr: 'parentId',
        })
        this.treeData = treeData

        this.defaultExpandedKeys.push(treeData[0].deptId)
        // 触发父组件中 selectTree
        this.$emit(
          'selectTree',
          treeData[0].deptId,
          treeData[0].fullName,
          treeData[0].org_cascade + ':' + treeData[0].level_num,
        )
      })
    },
    fommat ({
      arrayList,
      pidStr = 'parentId',
      idStr = 'deptId',
      childrenStr = 'children',
    }) {
      const listOjb = {} // 用来储存{key: obj}格式的对象
      const treeList = [] // 用来储存最终树形结构数据的数组
      // 将数据变换成{key: obj}格式，方便下面处理数据
      for (let i = 0; i < arrayList.length; i++) {
        var data = arrayList[i]
        data.title = data.fullName
        data.key = data.deptId
        data.value = data.org_cascade + ':' + data.level_num
       // console.log(data.child_num);
        if (data.child_num == 0) {
          data.isLeaf = true
        }
        listOjb[arrayList[i][idStr]] = data
      }
      // 根据pid来将数据进行格式化
      for (let j = 0; j < arrayList.length; j++) {
        // 判断父级是否存在
        const haveParent = listOjb[arrayList[j][pidStr]]
        if (haveParent) {
          // 如果有没有父级children字段，就创建一个children字段
          !haveParent[childrenStr] && (haveParent[childrenStr] = [])
          // 在父级里插入子项
          haveParent[childrenStr].push(arrayList[j])
        } else {
          // 如果没有父级直接插入到最外层
          treeList.push(arrayList[j])
        }
      }
      return treeList
    },
  },
}
</script>
